package com.gwclh.personnel.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.Station;
import com.gwclh.personnel.service.IStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位表
 * 前端控制器
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@RestController
@RequestMapping("/stations")
@Api(tags = "岗位管理")
public class StationController extends BaseController {

    @Autowired
    IStationService stationService;

    @GetMapping("/{id}")
    @ApiOperation(value = "获取选项里的信息")
    public ResultVo getStaExtId(@PathVariable Integer id) {
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", id);
        List<Station> stations = stationService.list(queryWrapper);
        Map<String,Object>result=new HashMap<>();
        result.put("stations",stations);
        return ResultVo.success(result);
    }

    @PutMapping("")
    @ApiOperation(value = "修改岗位信息")
    public ResultVo mdfSta(@RequestBody Station station) {
        return ResultVo.success(stationService.updateById(station));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单一岗位")
    public ResultVo delSta(@PathVariable(value = "id") Integer id) {

        Assert.isTrue(stationService.removeById(id), "删除岗位失败");
        return stationService.delSta(id);
    }

    @DeleteMapping("")
    @ApiOperation(value = "批量删除岗位")
    public ResultVo delListSta(@RequestBody(required = true) List<Integer> ids) {
        Assert.isTrue(stationService.removeByIds(ids), "批量删除岗位失败");
        return ResultVo.success();
    }

    @GetMapping("")
    @ApiOperation("分页展示岗位信息")
    public ResultVo listStaByPage(Station station, HttpServletRequest request) {
        PageDomain pageDomain = getPageParams(request);
        return stationService.listStations(pageDomain, station);
    }

    @PostMapping("")
    @ApiOperation(value = "增加岗位")
    public ResultVo insertSta(@Validated @RequestBody Station station) {
        return stationService.insertSta(station);
    }

    @GetMapping("/getStation/{id}")
    @ApiOperation(value = "根据岗位id获取岗位信息")
    public ResultVo getStationByid(@PathVariable Integer id){
        Station station=stationService.getById(id);
        return ResultVo.success(station);
    }
}
